package com.eshop.modules.mp.utils;

import java.util.HashMap;
import java.util.Map;

public class URLUtils
{
    public static String getParam(final String url, final String name) {
        return urlSplit(url).get(name);
    }
    
    private static String truncateUrlPage(String strURL) {
        String strAllParam = null;
        String[] arrSplit = null;
        strURL = strURL.trim().toLowerCase();
        arrSplit = strURL.split("[?]");
        if (strURL.length() > 1 && arrSplit.length > 1) {
            for (int i = 1; i < arrSplit.length; ++i) {
                strAllParam = arrSplit[i];
            }
        }
        return strAllParam;
    }
    
    public static Map<String, String> urlSplit(final String URL) {
        final Map<String, String> mapRequest = new HashMap<String, String>();
        String[] arrSplit = null;
        final String strUrlParam = truncateUrlPage(URL);
        if (strUrlParam == null) {
            return mapRequest;
        }
        final String[] split;
        arrSplit = (split = strUrlParam.split("[&]"));
        for (final String strSplit : split) {
            String[] arrSplitEqual = null;
            arrSplitEqual = strSplit.split("[=]");
            if (arrSplitEqual.length > 1) {
                mapRequest.put(arrSplitEqual[0], arrSplitEqual[1]);
            }
            else if (arrSplitEqual[0] != "") {
                mapRequest.put(arrSplitEqual[0], "");
            }
        }
        return mapRequest;
    }
}
